package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.pengjie.SnowflakeIdUtils;
import com.ruoyi.comprehensive.domain.DustPrevention;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.dto.DustPreventionExamineDto;
import com.ruoyi.comprehensive.mapper.DustPreventionMapper;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.vo.DustVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.DustPreventionExamineMapper;
import com.ruoyi.comprehensive.domain.DustPreventionExamine;
import com.ruoyi.comprehensive.service.IDustPreventionExamineService;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 防尘审核Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-15
 */
@Service
public class DustPreventionExamineServiceImpl implements IDustPreventionExamineService 
{
    @Autowired
    private DustPreventionExamineMapper dustPreventionExamineMapper;
    @Autowired
    private DustPreventionMapper dustPreventionMapper;
    @Autowired
    private PreventionEnclosureMapper preventionEnclosureMapper;
    /**
     * 查询防尘审核
     * 
     * @param id 防尘审核主键
     * @return 防尘审核
     */
    @Override
    public DustPreventionExamine selectDustPreventionExamineById(String id)
    {
        return dustPreventionExamineMapper.selectDustPreventionExamineById(id);
    }

    /**
     * 查询防尘审核列表
     * 
     * @param dustPreventionExamine 防尘审核
     * @return 防尘审核
     */
    @Override
    public List<DustPreventionExamine> selectDustPreventionExamineList(DustPreventionExamine dustPreventionExamine)
    {
        return dustPreventionExamineMapper.selectDustPreventionExamineList(dustPreventionExamine);
    }

    /**
     * 新增防尘审核
     * 
     * @param dustPreventionExamine 防尘审核
     * @return 结果
     */
    @Override
    public int insertDustPreventionExamine(DustPreventionExamine dustPreventionExamine)
    {
        dustPreventionExamine.setId(String.valueOf(new SnowflakeIdUtils(5,8).nextId()));
        return dustPreventionExamineMapper.insertDustPreventionExamine(dustPreventionExamine);
    }

    /**
     * 修改防尘审核
     * 
     * @param dustPreventionExamine 防尘审核
     * @return 结果
     */
    @Override
    public int updateDustPreventionExamine(DustPreventionExamine dustPreventionExamine)
    {
        DustPreventionExamine dustPreventionExamine1 = new DustPreventionExamine();
        BeanUtils.copyProperties(dustPreventionExamine,dustPreventionExamine1);
        dustPreventionExamine1.setUpdateAt(new Date());
        dustPreventionExamine1.setUpdateByName(SecurityUtils.getUsername());
        dustPreventionExamine1.setUpdateBy(SecurityUtils.getUserId().toString());
        DustPrevention dustPrevention = dustPreventionMapper.selectDustPreventionById(dustPreventionExamine.getDustPreventionId());
        if (dustPrevention != null){
            DustPrevention dustPrevention1 = new DustPrevention();
            dustPrevention1.setUpdateAt(new Date());
            dustPrevention1.setUpdateByName(SecurityUtils.getUsername());
            dustPrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
            dustPrevention1.setExamineStatus(dustPreventionExamine.getExamineStatus());
            dustPreventionMapper.updateDustPrevention(dustPrevention1);
        }
        return dustPreventionExamineMapper.updateDustPreventionExamine(dustPreventionExamine1);
    }

    /**
     * 批量删除防尘审核
     * 
     * @param ids 需要删除的防尘审核主键
     * @return 结果
     */
    @Override
    public int deleteDustPreventionExamineByIds(String[] ids)
    {
        return dustPreventionExamineMapper.deleteDustPreventionExamineByIds(ids);
    }

    /**
     * 删除防尘审核信息
     * 
     * @param id 防尘审核主键
     * @return 结果
     */
    @Override
    public int deleteDustPreventionExamineById(String id)
    {
        return dustPreventionExamineMapper.deleteDustPreventionExamineById(id);
    }

    /**
     * 查询防尘审核列表
     * @param vo
     * @return
     */
    @Override
    public PageInfo<DustPreventionExamineDto> getDustPreventionExamineList(DustVo vo) {
        startPage();
        List<DustPreventionExamine> dustPreventionExamineList = dustPreventionExamineMapper.getDustPreventionExamineList(vo);
        PageInfo dustExamineDtoInfo = new PageInfo<>(dustPreventionExamineList);
        List<DustPreventionExamineDto> list = dustPreventionExamineList.stream().map(dustPreventionExamine -> {
            DustPreventionExamineDto dustPreventionExamineDto = new DustPreventionExamineDto();
            BeanUtils.copyProperties(dustPreventionExamine, dustPreventionExamineDto);
            DustPrevention dustPrevention = dustPreventionMapper.selectDustPreventionById(dustPreventionExamine.getDustPreventionId());
            dustPreventionExamineDto.setDustPreventionDate(dustPrevention.getDustPreventionDate());
            dustPreventionExamineDto.setDustPreventionCode(dustPrevention.getDustPreventionCode());
            dustPreventionExamineDto.setName(dustPrevention.getName());
            return dustPreventionExamineDto;
        }).collect(Collectors.toList());
        dustExamineDtoInfo.setList(list);
        return dustExamineDtoInfo;
    }

    /**
     * 获取防尘审核详细信息
     * @param id
     * @return
     */
    @Override
    public DustPreventionExamineDto getDustPreventionExamineById(String id) {
        DustPreventionExamineDto dustPreventionExamineDto = new DustPreventionExamineDto();
        DustPreventionExamine dustPreventionExamine = dustPreventionExamineMapper.selectDustPreventionExamineById(id);
        DustPrevention dustPrevention = dustPreventionMapper.selectDustPreventionById(dustPreventionExamine.getDustPreventionId());
        PreventionEnclosure preventionEnclosure = new PreventionEnclosure();
        preventionEnclosure.setPreventionId(dustPreventionExamine.getId());
        preventionEnclosure.setEnclosureType("4");
        BeanUtils.copyProperties(dustPreventionExamine,dustPreventionExamineDto);
        List<PreventionEnclosure> preventionEnclosures = preventionEnclosureMapper.selectPreventionEnclosureList(preventionEnclosure);
        dustPreventionExamineDto.setEnclosures(preventionEnclosures);
        dustPreventionExamineDto.setName(dustPrevention.getName());
        dustPreventionExamineDto.setDustPreventionDate(dustPrevention.getDustPreventionDate());
        dustPreventionExamineDto.setDustPreventionCode(dustPrevention.getDustPreventionCode());
        return dustPreventionExamineDto;
    }
}
